<template>
  <view class="tagSelector">
    <view class="tagSelector-content">
      <view class="color-scroll">
        <view class="color-content">
          <view
            class="color-item"
            v-for="color in colors"
            :key="color"
            :style="{ background: color }"
            :class="[activeColor === color ? 'is_active' : '']"
            @click="handleClickColor(color)"
          >
          </view>
        </view>
      </view>
      <!-- <span>分类图标</span> -->
      <view class="tag-content">
        <view class="category-scroll">
          <view class="category-content">
            <view
              class="category-item"
              :class="[activeCategory === item.category ? 'is_active' : '']"
              v-for="item in tags"
              :key="item.category"
              @click="handleClickCategory(item)"
            >
              {{ item.title }}
            </view>
          </view>
        </view>
        <view class="icons-scroll">
          <view class="icons-content">
            <view
              class="icons-content__c"
              v-for="item in tags"
              :key="item.category"
            >
              <view class="icons-category">{{ item.title }}</view>
              <view class="icon-box">
                <view
                  class="icon-item"
                  :class="[activeIcon === icon ? 'is_active' : '']"
                  v-for="icon in item.icons"
                  :key="icon"
                  @click="handleClickIcon(item, icon)"
                >
                  <i class="icon iconfont" :class="icon"></i>
                </view>
              </view>
            </view>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>
<script lang="ts">
import tags from './icon'
import colors from './color'
import { Icon } from './type'
import Vue from 'vue'
export default Vue.extend({
  data() {
    return {
      activeColor: this.iconColor || '#ffc928',
      activeCategory: 'eat',
      activeIcon: this.icon || 'icon-canyin',
      tags,
      colors,
    }
  },
  props: {
    icon: {
      type: String,
      default: colors[Math.floor(Math.random()*32)],
    },
    iconColor: {
      type: String,
      default: 'icon-canyin',
    },
  },
  methods: {
    // 选择颜色
    handleClickColor(color: string) {
      this.activeColor = color
      this.$emit('change', { icon: this.activeIcon, color })
    },
    // 选择分类
    handleClickCategory(icons: Icon) {
      this.activeCategory = icons.category
    },
    // 选择图标
    handleClickIcon(icons: Icon, icon: string) {
      this.activeCategory = icons.category
      this.activeIcon = icon
      this.$emit('change', { icon, color: this.activeColor })
    },
    // 获取图标及颜色
    getTagAttr() {
      return { icon: this.activeIcon, color: this.activeColor }
    },
  },
})
</script>
<style lang="less">
.tagSelector-content {
  height: 100%;
  background: #fff;
  border-top-right-radius: 30rpx;
  border-top-left-radius: 30rpx;
  span {
    font-size: 24rpx;
    color: #8d8d8d;
    margin-left: 24rpx;
    height: 30rpx;
    line-height: 30rpx;
    margin-bottom: 20rpx;
    display: inline-block;
  }
  .color-scroll {
    height: 120rpx;
    width: 100vw;
    background: #fff;
    overflow-x: scroll;
    box-sizing: border-box;
    border-top-left-radius: 30rpx;
    border-top-right-radius: 30rpx;
    .color-content {
      margin-top: 30rpx;
      display: flex;
      flex-direction: row;
      .color-item {
        width: 60rpx;
        height: 60rpx;
        border-radius: 50%;
        flex-shrink: 0;
        margin: 0 20rpx;
        &.is_active {
          position: relative;
          &:after {
            content: '';
            position: absolute;
            left: 16rpx;
            top: 18rpx;
            width: 40%;
            height: 20%;
            border: 4rpx solid #fff;
            border-radius: 2rpx;
            border-top: none;
            border-right: none;
            background: transparent;
            transform: rotate(-45deg);
          }
        }
      }
    }
  }
  .tag-content {
    width: 100vw;
    display: flex;
    flex-direction: row;
    height: calc(100% - 120rpx);
    .category-scroll {
      flex-shrink: 0;
      width: 80rpx;
      overflow-y: scroll;
      margin-right: 40rpx;
      .category-content {
        .category-item {
          width: 80rpx;
          height: 80rpx;
          border-radius: 10rpx;
          margin-bottom: 6rpx;
          font-size: 28rpx;
          color: #8d8d8d;
          background: #f6f7f9;
          text-align: center;
          vertical-align: middle;
          line-height: 80rpx;
          &:last-child {
            margin-bottom: 0;
          }
          &.is_active {
            background: #fff;
            color: #69c9d7;
          }
        }
      }
    }
    .icons-scroll {
      overflow-y: scroll;
      .icons-content {
        .icons-content__c {
          &:first-child {
            .icons-category {
              display: none;
            }
          }
          .icons-category {
            color: #747474;
            font-size: 24rpx;
            margin-bottom: 10rpx;
          }
          .icon-box {
            margin-bottom: 30rpx;
            &:last-child {
              margin-bottom: 0;
            }
            .icon-item {
              width: 100rpx;
              height: 100rpx;
              border-radius: 20rpx;
              background: #f6f7f9;
              display: inline-flex;
              justify-content: center;
              align-items: center;
              margin: 0 20rpx 20rpx 0;
              &.is_active {
                background: #bfc0c2;
                .iconfont {
                  color: #fff;
                }
              }
              .iconfont {
                color: #aaaaaa;
                font-size: 60rpx;
              }
            }
          }
        }
      }
    }
  }
}
</style>